
/*
 *
 * Copyright (C) 2008 Mattias Blomqvist, patr-blo at dsv dot su dot se
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using FlowLib.Containers;
using FlowLib.Events;
using ClientExample.Containers;

namespace ClientExample.Controls
{
    public partial class SearchBox : UserControl
    {
        public event FmdcEventHandler ActionUpdate;

        protected string defaultText = string.Empty;
        protected bool isDefault = false;
        protected DateTime lastSearch = DateTime.MinValue;
        protected int minInterval = 1;

        public string DefaultText
        {
            get { return GetDefaultText(); }
        }

        public AutoCompleteStringCollection AutoCompleteSource
        {
            get
            {
                return textBox1.AutoCompleteCustomSource;
            }
        }

        public SearchBox()
        {
			IntPtr h = Handle;  // This is for stupid mono stuff. Some how handle isnt created correctly if we dont do this :(
            InitializeComponent();
            ActionUpdate = new FmdcEventHandler(OnActionUpdate);

            pictureBox1.Image = Program.Images.GetBitmap("search.gif");

            textBox1.GotFocus += new EventHandler(textBox1_GotFocus);
            textBox1.LostFocus += new EventHandler(textBox1_LostFocus);
            pictureBox1.MouseClick += new MouseEventHandler(pictureBox1_MouseClick);

            //AutoCompleteSource.Add("Flow84 is the creator of this Application");

            SetTextBox(false);

            textBox1.KeyUp += new System.Windows.Forms.KeyEventHandler(textBox1_KeyUp);
            if (Program.Settings != null)
                Program.Settings.SettingsUpdated += new FmdcEventHandler(Settings_SettingsUpdated);
        }

        void Settings_SettingsUpdated(object sender, FmdcEventArgs e)
        {
            if (isDefault)
                textBox1.Text = GetDefaultText();
        }

        void OnActionUpdate(object sender, FmdcEventArgs e) { }

        void textBox1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            e.SuppressKeyPress = true;
            if (e.KeyCode == System.Windows.Forms.Keys.Enter)
            {
                StartSearch();
            }
        }

        void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                // Open settings dialog
                ActionUpdate(this, new FmdcEventArgs(EventActions.SearchExtended));
            }
            else if (e.Button == MouseButtons.Left)
            {
                // Start search
                StartSearch();
            }
        }

        protected void SetTextBox(bool focus)
        {
            isDefault = !focus;
            if (focus)
            {
                textBox1.ForeColor = Color.Black;
                if (textBox1.Text.Equals(GetDefaultText()))
                    textBox1.Text = string.Empty;
                //pictureBox1.Visible = false;
            }
            else
            {
                //pictureBox1.Visible = true;
                textBox1.ForeColor = Color.LightGray;
                if (textBox1.TextLength == 0)
                    textBox1.Text = GetDefaultText();
            }
        }

        protected string GetDefaultText()
        {
            string strType = string.Empty;
            if (Program.Settings == null)
                return string.Empty;
            switch (Program.Settings.SearchType)
            {
                case SearchTypes.Spy:
                    strType = "See others searches...";
                    break;
                case SearchTypes.User:
                    strType = "Search for user...";
                    break;
                case SearchTypes.Content:
                default:
                    strType = "Search for content...";
                    break;
            }
            return strType;
        }

        void textBox1_LostFocus(object sender, EventArgs e)
        {
            SetTextBox(false);
        }

        void textBox1_GotFocus(object sender, EventArgs e)
        {
            SetTextBox(true);
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
        }

        public void StartSearch()
        {
            if (DateTime.Now > lastSearch.AddMinutes(minInterval))
            {
                SearchInfo info = new SearchInfo();
                info.Set(SearchInfo.TYPE, Program.Settings.SearchType.ToString());
                info.Set(SearchInfo.TOKEN, DateTime.Now.Ticks.ToString());
                info.Set(SearchInfo.SEARCH, textBox1.Text.TrimEnd('\n').TrimEnd('\r'));
                ActionUpdate(this, new FmdcEventArgs(EventActions.Search, info));
                textBox1.Clear();
                textBox1.Focus();
                lastSearch = DateTime.Now;
            }
            else
            {
                TimeSpan ts = lastSearch.AddMinutes(minInterval).Subtract(DateTime.Now);
                string msg = String.Format("You are only allowed to make ONE search every {0} min.\r\nTime left: {1}min {2}sec {3}ms", minInterval, ts.Minutes, ts.Seconds, ts.Milliseconds);

                MessageBox.Show(msg, "Information", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
    }
}
